package cn.neusoft.service.impl;

import cn.neusoft.domain.Dept;
import cn.neusoft.domain.Warehouse;
import cn.neusoft.enums.Constants;
import cn.neusoft.enums.Role;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.mapper.WarehouseMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.DeptService;
import cn.neusoft.service.WarehouseService;
import cn.neusoft.util.EnumUtils;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    DeptService deptService;

    @Autowired
    WarehouseMapper warehouseMapper;

    @Override
    @Transactional
    public void add(Warehouse warehouse) {

        Integer warehouseLevel = warehouse.getWarehouse_level();

        // 库房级别非法
        if (!EnumUtils.isInclude(Role.class, warehouseLevel)) {
            throw new AppException(AppExceptionCodeMsg.WAREHOUSE_LEVEL_INVALID);
        }

        // 需要：warn_value、max_value > 0 and warn_value <= max_value
        Long warn_value = warehouse.getWarn_value();
        Long max_value = warehouse.getMax_value();
        if(warn_value <= 0 || max_value <= 0 || warn_value >= max_value){
            throw new AppException(AppExceptionCodeMsg.WAREHOUSE_QUOTA_INVALID);
        }

        if(Objects.equals(warehouseLevel, 0)){  // 要添加的是中心仓库
            warehouse.setParent_id(Constants.CENTER_WAREHOUSE_PARENT_ID.getCode());
        }else { // 要添加的是分库
            warehouse.setParent_id(Constants.SUB_WAREHOUSE_PARENT_ID.getCode());
        }

        Dept dept = copyInfoToDept(warehouse);
        deptService.add(dept);

        warehouse.setWarehouse_id(dept.getDept_id());

        warehouseMapper.add(warehouse);
    }

    @Override
    @Transactional
    public void deleteById(Long warehouse_id) {
        deptService.deleteById(warehouse_id);
        warehouseMapper.deleteById(warehouse_id);
    }

    @Override
    @Transactional
    public void update(Warehouse warehouse) {

        Integer warehouseLevel = warehouse.getWarehouse_level();

        // 库房级别非法
        if (!EnumUtils.isInclude(Role.class, warehouseLevel)) {
            throw new AppException(AppExceptionCodeMsg.WAREHOUSE_LEVEL_INVALID);
        }

        // 需要：warn_value、max_value > 0 and warn_value <= max_value
        Long warn_value = warehouse.getWarn_value();
        Long max_value = warehouse.getMax_value();
        if(warn_value <= 0 || max_value <= 0 || warn_value >= max_value){
            throw new AppException(AppExceptionCodeMsg.WAREHOUSE_QUOTA_INVALID);
        }

        if(Objects.equals(warehouseLevel, 0)){  // 是中心仓库
            warehouse.setParent_id(Constants.CENTER_WAREHOUSE_PARENT_ID.getCode());
        }else { // 是分库
            warehouse.setParent_id(Constants.SUB_WAREHOUSE_PARENT_ID.getCode());
        }

        Dept dept = copyInfoToDept(warehouse);
        deptService.update(dept);

        warehouseMapper.update(warehouse);
    }

    @Override
    @Transactional
    public Warehouse findById(Long warehouse_id) {
        return warehouseMapper.findById(warehouse_id);
    }

    @Override
    public List<Warehouse> findAll() {
        return warehouseMapper.findAll();
    }

    @Override
    @Transactional
    public PageBean<Warehouse> findByPage(BaseQuery query) {
        // 构建结果对象
        PageBean<Warehouse> pageBean = new PageBean<>();

        // 开启分页
        PageHelper.startPage(query.getPageNow(), query.getPageSize());

        // 查询得到当前页的集合
        List<Warehouse> warehouseList = warehouseMapper.findByCondition(query);

        // 创建一个分页的实体PageInfo
        PageInfo<Warehouse> pageInfo = new PageInfo<>(warehouseList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }

    @Override
    @Transactional
    public List<String> getDistinctAddress() {
        return warehouseMapper.getDistinctAddress();
    }

    private Dept copyInfoToDept(Warehouse warehouse){

        Dept dept = new Dept();
        dept.setDept_id(warehouse.getWarehouse_id());
        dept.setDept_name(warehouse.getWarehouse_name());
        dept.setParent_id(warehouse.getParent_id());
        dept.setDept_addr(warehouse.getWarehouse_addr());
        dept.setLeader(warehouse.getLeader());
        dept.setPhone(warehouse.getWarehouse_tel());
        dept.setStatus(warehouse.getStatus());
        dept.setCreate_by(warehouse.getCreate_by());
        dept.setUpdate_by(warehouse.getUpdate_by());

        return dept;
    }
}
